﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class EditableShops
    {
        partial void gridAddAndEditNew_CanExecute(ref bool result)
        {
            // Write your code here.

        }

        partial void gridAddAndEditNew_Execute()
        {
            // Write your code here.
            this.Application.ShowCreateNewShop(0,0);
        }

        partial void ImportFromExcel_Execute()
        {
            ExcelImporter.Importer.ImportFromExcel(ShopsByName);
        }

        partial void gridEditSelected_CanExecute(ref bool result)
        {
            // Write your code here.

        }

        partial void gridEditSelected_Execute()
        {
            if (ShopsByName.SelectedItem != null) this.Application.ShowShopDetail(ShopsByName.SelectedItem.Id);
        }

        partial void EditableShops_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            //if (this.Application.User.HasPermission(Permissions.CanEditShop))//admin
            //{
            //}
            //else if (this.Application.User.HasPermission(Permissions.CanManageMyShop))//sales person
            //{
            //    //for some reason LINQ does not work with external datasource link
            //    //var currentSalesPerson = this.DataWorkspace.ApplicationData.SalesPersons.Where(s => s.aspnet_Profile.PropertyValuesString == userName).FirstOrDefault();
            //    //if (currentSalesPerson != null)
            //    //{
            //    //    this.SalesPersonId = currentSalesPerson.Id;
            //    //    this.VisitProperty.SalesPerson = currentSalesPerson;
            //    //    this.FindControl("SalesPerson").IsReadOnly = true;
            //    //}
            //    string userName = this.Application.User.FullName;
            //    //sales persons
            //    foreach (SalesPerson item in this.DataWorkspace.ApplicationData.SalesPersons)
            //    {
            //        if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
            //        {
            //            this.SalesPersonId = item.Id;
            //            break;
            //        }
            //    }
            //    //customers
            //    foreach (Customer item in this.DataWorkspace.ApplicationData.Customers.Where(c => c.aspnet_Profile_UserId != null))
            //    {
            //        if (item.aspnet_Profile != null && item.aspnet_Profile.PropertyValuesString == userName)
            //        {
            //            this.CustomerId = item.Id;
            //            break;
            //        }
            //    }
            //}
        }
    }
}
